<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Warehouse as WarehouseModel;
class Warehouse extends BaseController
{
   public function index($limit=10)
   {
       $name = request()->param('name');
       $status = request()->param('status',1);
       $time = request()->param('time');
       $school = request()->param('school_id');
       $data = WarehouseModel::alias('w')
           ->leftJoin('products p','p.id=w.product_id')
           ->where('w.school_id', $school)
           ->where('w.status',$status)
           ->when($name, function ($query) use ($name) {
               $query->where('p.name', 'like', "%$name%");
           })
           ->when($status, function ($query) use ($status) {
               $query->where('w.status', $status);
           })->when($time, function ($query) use ($time) {
               $query->whereBetween('w.create_time', $time);
           })->field('w.*,p.name,p.number')
           ->order('w.id','desc')->paginate($limit);

       return $this->success($data);
   }

   public function update($id)
   {
       $params=request()->param();
       foreach ($params as $item){
           WarehouseModel::where('id',$item['id'])->update([
               'num'=>$item['points_num'],
               'points_num'=>$item['points_num'],
               'remark'=>$item['remark']
           ]);
       }

       return $this->success();
   }
}
